package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.FaEsmPline;
import com.ruoyi.system.mapper.FaEsmOrderinfoMapper;
import com.ruoyi.system.domain.FaEsmOrderinfo;
import com.ruoyi.system.service.IFaEsmOrderinfoService;

/**
 * 每日制令单Service业务层处理
 * 
 * @author colin
 * @date 2021-09-16
 */
@Service
public class FaEsmOrderinfoServiceImpl implements IFaEsmOrderinfoService 
{
    @Autowired
    private FaEsmOrderinfoMapper faEsmOrderinfoMapper;

    /**
     * 查询每日制令单
     * 
     * @param id 每日制令单主键
     * @return 每日制令单
     */
    @Override
    public FaEsmOrderinfo selectFaEsmOrderinfoById(String id)
    {
        return faEsmOrderinfoMapper.selectFaEsmOrderinfoById(id);
    }

    /**
     * 查询每日制令单列表
     * 
     * @param faEsmOrderinfo 每日制令单
     * @return 每日制令单
     */
    @Override
    public List<FaEsmOrderinfo> selectFaEsmOrderinfoList(FaEsmOrderinfo faEsmOrderinfo)
    {
        return faEsmOrderinfoMapper.selectFaEsmOrderinfoList(faEsmOrderinfo);
    }

    /**
     * 新增每日制令单
     * 
     * @param faEsmOrderinfo 每日制令单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFaEsmOrderinfo(FaEsmOrderinfo faEsmOrderinfo)
    {
        faEsmOrderinfo.setCreateTime(DateUtils.getNowDate());
        int rows = faEsmOrderinfoMapper.insertFaEsmOrderinfo(faEsmOrderinfo);
        insertFaEsmPline(faEsmOrderinfo);
        return rows;
    }

    /**
     * 修改每日制令单
     * 
     * @param faEsmOrderinfo 每日制令单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFaEsmOrderinfo(FaEsmOrderinfo faEsmOrderinfo)
    {
        faEsmOrderinfo.setUpdateTime(DateUtils.getNowDate());
        faEsmOrderinfoMapper.deleteFaEsmPlineBySn(faEsmOrderinfo.getId());
        insertFaEsmPline(faEsmOrderinfo);
        return faEsmOrderinfoMapper.updateFaEsmOrderinfo(faEsmOrderinfo);
    }

    /**
     * 批量删除每日制令单
     * 
     * @param ids 需要删除的每日制令单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFaEsmOrderinfoByIds(String[] ids)
    {
//        faEsmOrderinfoMapper.deleteFaEsmPlineBySns(ids);
        return faEsmOrderinfoMapper.deleteFaEsmOrderinfoByIds(ids);
    }

    /**
     * 删除每日制令单信息
     * 
     * @param id 每日制令单主键
     * @return 结果
     */
    @Override
    public int deleteFaEsmOrderinfoById(String id)
    {
        faEsmOrderinfoMapper.deleteFaEsmPlineBySn(id);
        return faEsmOrderinfoMapper.deleteFaEsmOrderinfoById(id);
    }

    /**
     * 新增产线信息
     * 
     * @param faEsmOrderinfo 每日制令单对象
     */
    public void insertFaEsmPline(FaEsmOrderinfo faEsmOrderinfo)
    {
        List<FaEsmPline> faEsmPlineList = faEsmOrderinfo.getFaEsmPlineList();
        String id = faEsmOrderinfo.getId();
        if (StringUtils.isNotNull(faEsmPlineList))
        {
            List<FaEsmPline> list = new ArrayList<FaEsmPline>();
            for (FaEsmPline faEsmPline : faEsmPlineList)
            {
                faEsmPline.setSn(id);
                list.add(faEsmPline);
            }
            if (list.size() > 0)
            {
                faEsmOrderinfoMapper.batchFaEsmPline(list);
            }
        }
    }
}
